<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */
declare (strict_types=1);

namespace lflow\assignment\ipml;

use lflow\helper\AssertHelper;
use lflow\model\TaskModel;
use lflow\assignment\Assignment;
use lflow\assignment\AssignmentHandler;
use lflow\workflow\Execution;
use lflow\workflow\WorkFlowEngine;
use think\facade\Db;

/**
 * NamedUser
 * 指定用户
 *
 * @author Mr.April
 * @since  1.0
 */
class NamedUser extends Assignment implements AssignmentHandler
{

    private string $assingKey = self::APPROVE_ASSIGN;

    private static string $QUERY_SYS_USER = "SELECT id,user_name,real_name FROM sys_user";

    public function assign(TaskModel $model, Execution $execution): string
    {
        $assing = $model->getAssignee();
        $engine = new WorkFlowEngine();
        AssertHelper::notNull($assing, '用户[' . $assing . '不存在]');
        $where  = " WHERE is_deleted = 1";
        $where  .= " and id in (";
        $where  .= str_repeat("?,", count(explode(',', $assing)));
        $where  = substr($where, 0, strlen($where) - 1);
        $where  .= ") ";
        $result = $engine->query()->access()->queryList('', self::$QUERY_SYS_USER . $where, [$assing]);
        AssertHelper::notNull((object)$result, '用户[' . $assing . '不存在或已离职]');
        return implode(',', array_column($result, 'id'));
    }
}
